package com.ghck.server.service.impl;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.ghck.message.model.JPushNotification;
import com.ghck.message.service.JpushService;
import com.ghck.server.dao.MessageMapper;
import com.ghck.server.exception.ErrorConstant;
import com.ghck.server.exception.GhckException;
import com.ghck.server.model.Message;
import com.ghck.server.model.response.UserRes;
import com.ghck.server.service.MessageService;
import com.ghck.server.service.UsersService;
import com.ghck.server.utils.FileStoreUtil;
import com.ghck.server.utils.JPushUtils;

import cn.jpush.api.push.model.PushPayload;

@Service
@Transactional
public class MessageServiceImpl implements MessageService{

	private static final Logger logger = LoggerFactory.getLogger(MessageServiceImpl.class);
	
	@Autowired
	private MessageMapper messageMapper;
	@Autowired
	private UsersService userService;
	
	@Autowired
	private JpushService jpushService;
	
	@Override
	public void pushMessage(Message message) {
		if(StringUtils.isEmpty(message.getTitle())){
			throw new GhckException(ErrorConstant.PARAM_ERROR.getErrorCode(), "标题不能为空");
		}
		if(StringUtils.isEmpty(message.getContent())){
			throw new GhckException(ErrorConstant.PARAM_ERROR.getErrorCode(),"内容不能为空");
		}
		// 1 保存信息
		messageMapper.insert(message);
		//查询所有用户
		List<UserRes> userList = userService.getAllUsers();
		for(UserRes user : userList){
			message.setUserId(user.getId());
			//保存用户是否已读信息
			messageMapper.insertUserMeesage(message);
		}
		//推送消息
		if(null != message.getIsPush() && message.getIsPush() == 1){
			JPushNotification notification=new JPushNotification();
			notification.setAlert(message.getTitle());
			notification.setTitle(message.getTitle());
			//1、增加业务参数
			Map<String,Object> map=new HashMap<String,Object>();
			//参考 http://stage.boyibang.com/wiki/doku.php?id=%E6%8E%A8%E9%80%81%E7%9B%B8%E5%85%B3
			String url = FileStoreUtil.getBaseUrl().concat("message/detail").concat("/").concat(String.valueOf(message.getId())).concat("/").concat(String.valueOf(message.getrId()));
			map.put("url", url);
			notification.setExtras(map);
			//2、设置platform (推送的平台：1、ios 2、android 3、ios_android (非空字段))
			notification.setPlatform(3);
			notification.setAppId(1);
//			notification.setAlias(user.getAccessId());
			logger.info("调用mc开始，入参是{}",JSON.toJSONString(notification));
			jpushService.sendNotification(notification);
		}
	}
	@Override
	public List<Message> list(Message message) {
		
		return messageMapper.list(message);
	}
	@Override
	public List<Message> myList(Message message) {
		List<Message> list = messageMapper.myList(message);
		for(Message m : list){
			m.setContent(null);
			m.setUrl(FileStoreUtil.getBaseUrl().concat("message/detail").concat("/").concat(String.valueOf(m.getId())).concat("/").concat(String.valueOf(m.getrId())));
		}
		return list;
	}
	
	@Override
	public Integer unreadCount(Integer userId) {
		return messageMapper.unreadCount(userId);
	}
	
	@Override
	public Integer markAsRead(Integer id) {
		return messageMapper.markAsRead(id);
	}
	@Override
	public Message detail(Integer id) {
		return messageMapper.findById(id);
	}
	
	
}